<?php 
if(!defined('ADMINPX') || !defined('ADMIN')) {
	exit('Access Denied');
}
$id=$_GET[id]; 
$act=$_GET['act'];
$web=$_GET['web'];
$yid=$_GET['yid']; 
$mm=$_GET['mm'];
$templates=$_GET['templates'];

if($act == 'templates'){
       echo"<div class=\"admin\">【{$templates}】模板页面设计</div>"; 
       $sqlfile ="templates/$templates/";
       $dir = @opendir("$sqlfile");
       while ($file = @readdir($dir)){
		      if( preg_match('/.htm/mis',$file)){
              echo "$file <a href=\"admin.php?action=templates&act=xg&mm=$file&templates=$templates\"><font color=\"#ff0000\"><b>设计</b></a></font><br />";
          }
       }

       $sqlfile ="templates/$templates/";
       $dir = @opendir("$sqlfile");
       while ($file = @readdir($dir)){
          if(in_array($file, $Bingz_xtmlpd) ) {
             echo " <b>[$file]</b><br />";
             $sqlfilek ="$sqlfile/$file/";
             $dirk = opendir("$sqlfilek");
             while ($filek = readdir($dirk)){
		            if( preg_match('/.htm/mis',$filek)){
                   echo "$filek <a href=\"admin.php?action=templates&act=xg&mm=$file/$filek&templates=$templates\"><font color=\"#ff0000\"><b>设计</b></a></font><br />";
                }             	    	
             }
          }
       }

}else if($act =='xgok'){
           $content=str_replace('\"','"',$_POST[name]);
           $content=str_replace("\'","'",$content);
           $content=str_replace('<!*{','<!--{',$content);
           $content=str_replace('}*>','}-->',$content);
  if($_POST['text'] ){
           $fp=fopen("templates/$templates/$mm","w");
           flock($fp,LOCK_EX);
           $write=fputs($fp,"$content");
           flock($fp,LOCK_UN);
           fclose($fp);
	         echo"编辑成功"; 
  }else if($_POST['textxd']){
           $fp=fopen("templates/$templates/".$mm.".txt","w");
           flock($fp,LOCK_EX);
           $write=fputs($fp,"$content");
           flock($fp,LOCK_UN);
           fclose($fp);
	         echo"备份成功"; 
  }else if($_POST['texthf']){
           $content = file_get_contents("templates/$templates/".$mm.".txt");
           $fp=fopen("templates/$templates/$mm","w");
           flock($fp,LOCK_EX);
           $write=fputs($fp,"$content");
           flock($fp,LOCK_UN);
           fclose($fp);
	         echo"恢复成功"; 
  }
  echo "<br/>正在自动返回.......<br/><a href=\"admin.php?action=templates&act=xg&web=$web&mm=$mm&templates=$templates\">请手动返回</a><br/><meta http-equiv=refresh content='3;url=admin.php?action=templates&act=xg&web=$web&mm=$mm&templates=$templates'>";
}else if($act =='xg'){
       $sqlfile ="templates/$templates/$mm";
       $content = file_get_contents("$sqlfile");
       $content =htmlentities($content, ENT_QUOTES,'utf-8');
echo <<<end
<form method="POST" action="admin.php?action=templates&&act=xgok&mm=$mm&templates=$templates"> 
<div class="admin">设计页面: (<b> $mm</b>)</div>
显示内容<br/>
<textarea  class="dktextarea" type="text" name="name" >$content</textarea><br/> 
<input class="submit" type="submit" value="编辑" name="text"/> <br/> 
<input class="submit" type="submit" value="备份" name="textxd"/> <br/> 
end;
        if(file_exists("templates/$templates/".$mm.".txt")){//判断备份
             echo '<input class="submit" type="submit" value="恢复" name="texthf"/> ';
             $a=filemtime("templates/$templates/".$mm.".txt");
             echo date("Y-m-d",$a);
        }else{
             echo '<font color="#ff0000">(没有备份文件)</font>';
        }
echo <<<end
</form></br>
注意：编辑前请先备份！</br>
end;
echo"<div class=\"admin_a\"><a href=\"admin.php?action=templates&act=templates&mm=$mm&templates=$templates\">返回文件目录</a></div>"; 
}else if($act =='bbok'){
  if(!$_POST[adminpost]){
	   echo"提交出错<br/>";
	}elseif($db->update('templates',"`wap`='$_POST[defaultnew2]' ,`wpc`='$_POST[defaultnew4]' , `web`='$_POST[defaultnew3]' ","`id`= '1' ")){
	   echo"设置成功<br/>";
	}else{
	   echo"设置失败<br/>";
	}
  echo "正在自动返回.......<br/><a href=\"admin.php?action=templates\">请手动返回</a><br/><meta http-equiv=refresh content='3;url=admin.php?action=templates'>";
}else{
echo"<div class=\"admin\">模板设置</div>"; 
$u=$db->get_one('templates'," `id` ='1'",'');
if($u[wap] == 'templates'){ $templatesa="CHECKED "; }
if($u[wpc] == 'templates'){ $templatesb="CHECKED "; }
if($u[web] == 'templates'){ $templatesc="CHECKED "; }
echo'
<form method="post" action="admin.php?action=templates&&act=bbok&web=$web&mm=$mm" >
		<div class="admin_a"><b>templates</b>    <a href="admin.php?action=templates&act=templates&templates=templates">页面设计</a><br/>
				 <input type="radio" class="radio" name="defaultnew2" value="templates" '.$templatesa.' /> wap
				 <input type="radio" class="radio" name="defaultnew4" value="templates" '.$templatesb.' /> wpc 
				 <input type="radio" class="radio" name="defaultnew3" value="templates" '.$templatesc.' /> web 
	  </div>		
';
  $dir = opendir("templates/");
  while($file = readdir($dir)){
		if($file !='templates' && $file !='.' && $file !='..' && !strpos($file,'.')){
			if($u[wap] == $file ){ $wap="CHECKED "; }
			if($u[wpc] == $file ){ $wpc="CHECKED "; }
			if($u[web] == $file ){ $web="CHECKED "; }
      echo'	
		    <div class="admin_a"><b>'.$file.'</b>    <a href="admin.php?action=templates&act=templates&templates='.$file.'">页面设计</a><br/>
				 <input type="radio" class="radio" name="defaultnew2" value="'.$file.'" '.$wap.' /> wap
				 <input type="radio" class="radio" name="defaultnew4" value="'.$file.'" '.$wpc.' /> wpc 
				 <input type="radio" class="radio" name="defaultnew3" value="'.$file.'" '.$web.' /> web 
	      </div>
      ';
		}
	} 
  closedir($dir);
echo'	

<input class="submit" type="submit" value="设置" name="adminpost"/> 
</form>
<br/>安装模板请上传到templates目录
';
}
echo"<div class=\"admin_a\"><a href=\"admin.php?action=templates\">返回模板设置</a></div>"; 
?>